<?php

namespace App\System\Log\Repositories;

use App\System\Base\Helpers\Traits\SinglePattern;
use App\System\Base\Repositories\Repository;
use App\System\Cis\Model\CisUser;
use App\System\Log\Model\LogDeclarationForm;


class LogDeclarationFormRepository extends Repository
{
    use SinglePattern;

    /**
     * 报单列表
     * @param string $keyword
     * @return mixed
     */
    public function getList($keyword=''){

        $data = LogDeclarationForm::when($keyword,function($query)use($keyword){
                return $query->where('id',$keyword);

            })
            ->orderBy('created_at', 'desc')
            ->paginate();
        if($data){
            foreach ($data as $k=>$v){
                $user_ids = explode(',',$v->generated_user_ids);
                $info = CisUser::where('id',$user_ids[0])->first();
                $v->parent = isset($info->mobile) ? $info->mobile : '';
            }
        }
        return $data;
    }

    /**
     * 报单详细
     * @param $id
     * @return bool|\Illuminate\Database\Eloquent\Model|null|static
     */
    public function getLog($id)
    {
        if(!$id){
            return false;
        }
        $data = LogDeclarationForm::where('id',$id)->first();
        $user_ids = explode(',',$data->generated_user_ids);
        $info = CisUser::where('id',$user_ids[0])->first();
        $data->parent = $info->mobile;
        return $data;
    }

    /**
     * 生成报单
     * @param array $user
     * @return int
     */
    public function CreateLog(array $data)
    {
        $res = LogDeclarationForm::create($data);
        if(!$res)
        {
            return false;
        }
        return $res->id;
    }
}